import axios from "axios";
import {Modal} from 'ant-design-vue';

axios.defaults.baseURL = '/gambler'
axios.defaults.timeout = 10000;

axios.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    response => {
        if (response.status === 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    error => {
        Modal.confirm({
            title: '请求异常',
            content: JSON.stringify(error),
            okText: '确认',
            cancelText: '取消',
        });
    }
);

export const post = (url, params = {}) => {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err)
            });
    })
}

export const get = (url, data) => {
    return new Promise((resolve, reject) => {
        axios.get(url, data)
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}


export default {
    install(app) {
        app.config.globalProperties.$post = (url, params) => post(url, params);
        app.config.globalProperties.$get = (url, params) => get(url, params)
    }
}